<template>
  <div>
    <a-input :value="value" @change="changeInput">
      <!-- <a-icon
        type="prefix"
        style="cursor: pointer;"
        @click="handleUser"
      /> -->
      <template #addonAfter>
        <UserOutlined @click="handleUser" />
      </template>
    </a-input>
    <a-modal
      v-model:visible.sync="modelVisible"
      :title="title"
      width="860px"
      append-to-body
      destroy-on-close
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <div>
        <a-form-model @keyup.enter="searchQuery" :label-col="labelCol" :wrapper-col="wrapperCol">
          <a-row :gutter="24">
            <a-col :md="6" :sm="12">
              <a-form-model-item label="名称">
                <a-input style="width: 100%" v-model="queryParam.label" placeholder="请输入名称" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="12">
              <a-form-model-item label="编码">
                <a-input style="width: 100%" v-model="queryParam.value" placeholder="请输入编码" />
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="8">
              <a-button type="primary" @click="searchQuery()">查询</a-button>
              <a-button style="margin-left: 4px" type="primary" @click="searchReset()"
                >重置</a-button
              >
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <a-table
        border
        fit
        size="small"
        key="validation-table"
        max-height="240"
        rowKey="value"
        :columns="columns"
        :data-source="dataList"
        :row-selection="{
          selectedRowKeys: selectedRowKeys,
          onChange: onSelectChange,
          type: multi
        }"
        :pagination="pagination"
        :loading="loading"
        @change="handleTableChange"
      >
      </a-table>
    </a-modal>
  </div>
</template>

<script>
import {
  UserOutlined,
} from '@ant-design/icons-vue';
export default {
  name: "TableSelect",
  props: {
    title: {
      type: String,
      default: "请选择"
    },
    api: {
      type: Function
    },
    value: String,
    multi: {
      type: String,
      default: "checkbox"
    }
  },
  model: {
    prop: "value",
    event: "change"
  },
  data() {
    return {
      dataListCopy: [],
      dataList: [],
      assignee: "",
      modelVisible: false,
      columns: [
        {
          title: "#",
          dataIndex: "",
          key: "rowIndex",
          width: 60,
          align: "center",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          }
        },
        {
          title: "名称",
          dataIndex: "label",
          key: "label"
        },
        {
          title: "编码",
          dataIndex: "value",
          key: "value"
        }
      ],
      pagination: {
        total: 0,
        pageSize: 10,
        current: 1,
        showTotal: (total, range) => {
          return range[0] + "-" + range[1] + " 共" + total + "条";
        },
        showQuickJumper: true,
        showSizeChanger: true
      },
      loading: false,
      selectedRowKeys: [],
      selectedRows: [],
      queryParam: {},
      labelCol: { span: 4 },
      wrapperCol: { span: 18 }
    };
  },
  methods: {
    async getDataList() {
      try {
        const parms = {
          query: {
            //查询参数
            ...this.queryParam
          },
          page: { ...this.pagination } //分页参数
        };
        this.loading = true;
        // 分页过滤参数，根据实际情况处理
        const res = await this.api(parms);
        this.loading = false;
        if (res instanceof Array) {
          this.dataList = res;
          this.dataListCopy = res;
          this.pagination = false;
        } else {
          this.pagination.total = res.total;
          this.dataList = res.result;
          this.dataListCopy = res.result;
        }
      } catch (error) {
        console.log(error);
      }
    },
    handleUser() {
      this.modelVisible = true;
    },
    handleOk() {
      this.$emit("change", this.selectedRowKeys.join(), this.selectedRows);
      this.close();
    },
    handleCancel() {
      this.close();
    },
    close() {
      this.modelVisible = false;
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectedRows = selectedRows;
    },
    searchQuery() {
      if (this.pagination) {
        this.pagination.current = 1;
        this.getDataList();
      } else {
        // 不分页时，前台处理过滤
        this.dataList = this.dataListCopy.filter(item => {
          return (
            (this.queryParam.label
              ? item.label.includes(this.queryParam.label)
              : true) &&
            (this.queryParam.value
              ? item.value.includes(this.queryParam.value)
              : true)
          );
        });
      }
    },
    searchReset() {
      this.queryParam = {};
      if (this.pagination) {
        this.pagination.current = 1;
      }
      this.getDataList();
    },
    changeInput(e) {
      this.$emit("change", e.target.value);
    },
    handleTableChange(pagination) {
      this.pagination = pagination;
      this.getDataList();
    }
  },
  mounted() {
    this.getDataList();
  },
  components: {
    UserOutlined
  }
};
</script>

<style></style>
